package org.chris.quick.b.fragments;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.MenuRes;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;

import org.chris.quick.R;
import org.chris.quick.tools.CommonUtils;
import org.chris.quick.tools.DevicesUtils;


/**
 * 请填写方法内容
 *
 * @author Chris zou
 * @Date 16/10/11
 * @modifyInfo1 chriszou-16/10/11
 * @modifyContent
 */
public abstract class ThemeFragment extends Fragment {
    public View appBaseLayoutContainer;/*根布局，内容*/
    protected FrameLayout appContentContainer;
    protected Toolbar appBaseToolbar;/*标题栏*/

    private Toolbar.OnMenuItemClickListener onMenuItemClickListener;
    private int resMenu = -1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (appBaseLayoutContainer == null) {
            if (isUsingBaseLayout()) {
                appBaseLayoutContainer = inflater.inflate(R.layout.app_content, container, false);
                appContentContainer = (FrameLayout) appBaseLayoutContainer.findViewById(R.id.appContent);
                inflater.inflate(onResultLayoutResId(), appContentContainer);
            } else {
                appBaseLayoutContainer = inflater.inflate(onResultLayoutResId(), container, false);
            }
            if (getToolbar() != null) {
                appBaseToolbar = getToolbar();
                if (hasTitle()) {
                    appBaseToolbar.setVisibility(View.VISIBLE);
                } else {
                    appBaseToolbar.setVisibility(View.GONE);
                }
            }
        } else {
            ViewGroup parent = (ViewGroup) appBaseLayoutContainer.getParent();
            if (parent != null) {
                parent.removeView(appBaseLayoutContainer);
            }
        }
        init();
        return appBaseLayoutContainer;
    }

    public Toolbar getToolbar() {
        if (isUsingBaseLayout() && appBaseToolbar == null)
            appBaseToolbar = (Toolbar) appBaseLayoutContainer.findViewById(R.id.mainToolbar);
        return appBaseToolbar;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        if (resMenu != -1)
            inflater.inflate(resMenu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (onMenuItemClickListener != null)
            return onMenuItemClickListener.onMenuItemClick(item);
        return super.onOptionsItemSelected(item);
    }

    public void setMenu(@MenuRes int resMenu, Toolbar.OnMenuItemClickListener onMenuItemClickListener) {
        this.resMenu = resMenu;
        this.onMenuItemClickListener = onMenuItemClickListener;
    }

    public void setTitle(String title) {
        appBaseToolbar.setTitle(title);
    }

    protected void setRightView(@LayoutRes int resId) {
        setRightView(LayoutInflater.from(getActivity()).inflate(resId, null), null);
    }

    protected void setRightView(View view) {
        setRightView(view, null);
    }

    protected void setRightView(View view, View.OnClickListener onClickListener) {
        checkNotNullToolbar();
        Toolbar.LayoutParams layoutParams = new Toolbar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
        if (onClickListener != null)
            view.setOnClickListener(onClickListener);
        appBaseToolbar.addView(view, layoutParams);
    }

    protected void setBackInvalid(int backIcon) {
        setBackValid(backIcon, false, null);
    }

    protected void setBackValid() {
        setBackValid(-1, true, null);
    }

    protected void setBackValid(int backIcon) {
        setBackValid(backIcon, true, null);
    }

    protected void setBackValid(View.OnClickListener onClickListener) {
        setBackValid(-1, true, onClickListener);
    }

    /**
     * @param backIcon        -1:默认按钮   其他为自定义按钮
     * @param isValid         单击按钮是否有效
     * @param onClickListener
     */
    protected void setBackValid(int backIcon, boolean isValid, View.OnClickListener onClickListener) {
        checkNotNullToolbar();
        if (backIcon == -1)
            appBaseToolbar.setNavigationIcon(R.mipmap.ic_back_arrow_white);
        else
            appBaseToolbar.setNavigationIcon(backIcon);

        if (isValid) {
            if (onClickListener == null) {
                onClickListener = new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getActivity().onBackPressed();
                    }
                };
            }
            appBaseToolbar.setNavigationOnClickListener(onClickListener);
        }
    }

    public void setOnClickListener(View.OnClickListener onClickListener, @IdRes int... resIds) {
        for (int resId : resIds) {
            setOnClickListener(onClickListener, getView(resId));
        }
    }

    public void setOnClickListener(View.OnClickListener onClickListener, View... views) {
        for (View view : views)
            view.setOnClickListener(onClickListener);
    }

    private void checkNotNullToolbar() {
        if (appBaseToolbar == null) {
            return;
        }
    }

    /**
     * 获取View
     *
     * @param resId
     * @param <T>
     * @return
     */
    public <T extends View> T getView(@IdRes int resId) {
        return getView(resId, appBaseLayoutContainer);
    }

    /**
     * 获取View
     *
     * @param parent
     * @param resId
     * @param <T>
     * @return
     */
    public <T extends View> T getView(@IdRes int resId, View parent) {
        return (T) parent.findViewById(resId);
    }

    /**
     * 获取常规类型数值
     *
     * @param key
     * @param defaultValue
     * @param <T>
     * @return
     */
    public <T> T getTransmitValue(String key, T defaultValue) {
        return CommonUtils.getIntentValue(getActivity().getIntent(), key, defaultValue);
    }

    protected void showToast(CharSequence content) {
        showToast(content, 0, Toast.LENGTH_SHORT);
    }

    protected void showToast(CharSequence content, int gravity) {
        showToast(content, gravity, Toast.LENGTH_SHORT);
    }

    protected void showToast(CharSequence content, int gravity, int duration) {
        Toast toast = Toast.makeText(getActivity(), content, duration);
        if (gravity != 0)
            toast.setGravity(gravity, 0, 0);
        DevicesUtils.closeSoftInput(getActivity());
        toast.show();
    }

    protected void showSnackbar(CharSequence content) {

        showSnackbar(content, null, null);
    }

    protected void showSnackbar(View view, CharSequence content) {

        showSnackbar(view, content, null, null);
    }

    protected void showSnackbar(CharSequence content, View.OnClickListener onClickListener) {

        showSnackbar(content, getString(R.string.sure), onClickListener);
    }

    protected void showSnackbar(View view, CharSequence content, View.OnClickListener onClickListener) {

        showSnackbar(view, content, getString(R.string.sure), onClickListener);
    }

    protected void showSnackbar(CharSequence content, CharSequence actionTxt, View.OnClickListener onClickListener) {
        showSnackbar(null, content, actionTxt, onClickListener);
    }

    protected void showSnackbar(View view, CharSequence content, CharSequence actionTxt, View.OnClickListener onClickListener) {
        DevicesUtils.closeSoftInput(getActivity());
        if (view == null) {
            view = appBaseLayoutContainer;
        }
        Snackbar.make(view, content, Snackbar.LENGTH_SHORT).setAction(actionTxt, onClickListener).setActionTextColor(getResources().getColor(R.color.colorBlueShallow)).show();
    }

    /**
     * 返回资源文件ID
     *
     * @return
     */
    protected abstract
    @LayoutRes
    int onResultLayoutResId();

    /**
     * 是否有title
     *
     * @return
     */
    public boolean hasTitle() {
        return false;
    }

    /**
     * 初始化操作
     */
    public abstract void init();

    /**
     * 是否引用基本布局
     *
     * @return
     */
    public boolean isUsingBaseLayout() {
        return false;
    }
}
